package com.xqzgj.web.controller.system;

import com.xqzgj.common.core.controller.BaseController;
import com.xqzgj.common.core.domain.AjaxResult;
import com.xqzgj.common.core.page.TableDataInfo;
import com.xqzgj.system.domain.StockInfo;
import com.xqzgj.system.service.IStockAnalysisService;
import com.xqzgj.system.service.IStockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存分析 Controller
 */
@RestController
@RequestMapping("/system/stock/analysis")
public class StockAnalysisController extends BaseController {

    @Autowired
    private IStockAnalysisService stockAnalysisService;

    @Autowired
    private IStockInfoService stockInfoService;

    /**
     * 获取库存概览数据
     */
    @GetMapping("/overview")
    @PreAuthorize("@ss.hasPermi('system:stock:view')")
    public AjaxResult getOverview() {
        return success(stockAnalysisService.getInventoryOverview());
    }

    /**
     * 获取库存趋势数据
     */
    @GetMapping("/trend")
    @PreAuthorize("@ss.hasPermi('system:stock:view')")
    public AjaxResult getTrend(@RequestParam String startDate, @RequestParam String endDate) {
        return success(stockAnalysisService.getInventoryTrend(startDate, endDate));
    }

    /**
     * 获取库存对比数据
     */
    @GetMapping("/comparison")
    @PreAuthorize("@ss.hasPermi('system:stock:view')")
    public AjaxResult getComparison(@RequestParam String startDate, @RequestParam String endDate) {
        return success(stockAnalysisService.getInventoryComparison(startDate, endDate));
    }

    /**
     * 获取库存分类数据
     */
    @GetMapping("/category")
    @PreAuthorize("@ss.hasPermi('system:stock:view')")
    public AjaxResult getCategory() {
        return success(stockAnalysisService.getInventoryCategory());
    }

    /**
     * 获取库存变动类型分布
     */
    @GetMapping("/type/distribution")
    @PreAuthorize("@ss.hasPermi('system:stock:view')")
    public AjaxResult getTypeDistribution(@RequestParam String startDate, @RequestParam String endDate) {
        return success(stockAnalysisService.getInventoryTypeDistribution(startDate, endDate));
    }

    /**
     * 获取库存变动类型趋势
     */
    @GetMapping("/type/trend")
    @PreAuthorize("@ss.hasPermi('system:stock:view')")
    public AjaxResult getTypeTrend(@RequestParam String startDate, @RequestParam String endDate) {
        return success(stockAnalysisService.getInventoryTypeTrend(startDate, endDate));
    }

    /**
     * 更新预警阈值
     */
    @PutMapping("/warning/threshold")
    @PreAuthorize("@ss.hasPermi('system:stock:edit')")
    public AjaxResult updateWarningThreshold(@RequestParam Integer threshold) {
        return toAjax(stockAnalysisService.updateWarningThreshold(threshold));
    }

    /**
     * 导出分析报表
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('system:stock:export')")
    public AjaxResult exportAnalysisReport(@RequestParam String startDate, @RequestParam String endDate) {
        return success(stockAnalysisService.exportAnalysisReport(startDate, endDate));
    }

    /**
     * 获取商品库存分析列表
     */
    @GetMapping("/goods/list")
    @PreAuthorize("@ss.hasPermi('system:stock:view')")
    public TableDataInfo getGoodsList(StockInfo stockInfo) {
        startPage();
        List<StockInfo> list = stockInfoService.selectStockInfoList(stockInfo);
        return getDataTable(list);
    }
}